﻿@model Ymca.Web.Models.Blog.IndexViewModel

<h2>Blog</h2>

@if (Roles.IsUserInRole(User.Identity.Name, "Admin"))
{
	<a class="btn btn-primary" href="@Url.Action("Create", "Blog", new { backPage = ViewBag.BackPage })">Add</a>
}

@using (Html.BeginForm("Search", "Blog", FormMethod.Get, new { @class = "form-inline", role = "form" }))
{	
	<div class="form-group">
		@Html.TextBoxFor(model => model.Search, null, new { @class = "form-control", placeholder = "Search" })
	</div>
	<div class="form-group">
		<button type="submit" class="btn btn-primary" type="submit">Search</button>
	</div>
}
<br />
@if (Model.Posts != null && Model.Posts.Count() > 0)
{
	foreach(var post in Model.Posts){
		<div class="panel panel-default">
			<div class="panel-heading">
				<h3>
					<a href="@Url.Action("Details", new { id = post.Id })">
						@string.Format("{0} : {1}", post.Date.ToShortDateString(), post.Title)
					</a>
				</h3>
			</div>
			<div class="panel-body">
				@(new HtmlString(post.Body))
			</div>
		</div>
		if (Roles.IsUserInRole(User.Identity.Name, "Admin")){
			<div>
				<a class="btn" href="@Url.Action("Edit", "Blog", new { id = post.Id, backPage = ViewBag.BackPage })"><i class="icon-pencil"></i>Edit</a>
				<a class="btn" href="@Url.Action("Delete", "Blog", new { id = post.Id, backPage = ViewBag.BackPage })"><i class="icon-trash"></i>Delete</a>
			</div>
		}
		if(post.Comments != null && post.Comments.Count > 0){
			var showingCount = post.Comments.Count > 10 ? 10 : post.Comments.Count();
		}
	}
}
else
{
	<div class="panel panel-default">
		<div class="panel-body">
			There is no any posts.
		</div>
	</div>
}